package com.lly.songhigh.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.lly.songhigh.R;
import com.lly.songhigh.entity.MVDetials;

import java.util.List;

import it.sephiroth.android.library.picasso.Picasso;

/**
 * Created by 李海龙 on 15/10/28.
 */
public class MVDetialsListViewAdapter extends BaseAdapter {

    private Context context;
    private List<MVDetials.DataEntity> dataEntities;

    public MVDetialsListViewAdapter(Context context, List<MVDetials.DataEntity> dataEntities) {
        this.context = context;
        this.dataEntities = dataEntities;
    }

    public List<MVDetials.DataEntity> getDataEntities() {
        return dataEntities;
    }

    public void refreshData(List<MVDetials.DataEntity> dataEntities){
        this.dataEntities = dataEntities;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return dataEntities != null && dataEntities.size() > 0 ? dataEntities.size() : 0;
    }

    @Override
    public MVDetials.DataEntity getItem(int position) {
        return dataEntities.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder;

        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.mvdetialslistview_item, null);
            holder = new ViewHolder();
            holder.mvDetialsItemPicture = (ImageView) convertView.findViewById(R.id.mv_detials_item_picture);
            holder.mvDetialsItemSongName = (TextView) convertView.findViewById(R.id.mv_detials_item_songName);
            holder.mvDetialsItemSingerName = (TextView) convertView.findViewById(R.id.mv_detials_item_singerName);
            holder.mvDetialsItemCommentCount = (TextView) convertView.findViewById(R.id.mv_detials_item_commentCountTv);
            holder.mvDetialsItemPlayCount = (TextView) convertView.findViewById(R.id.mv_detials_item_playCountTv);
            holder.mvDetialsItemTime = (TextView) convertView.findViewById(R.id.mv_detials_item_time);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        MVDetials.DataEntity dataEntity = dataEntities.get(position);
        holder.mvDetialsItemPlayCount.setText(dataEntity.getPickCount() + "");
        holder.mvDetialsItemCommentCount.setText(dataEntity.getBulletCount() + "");
        holder.mvDetialsItemSingerName.setText(dataEntity.getSingerName());
        holder.mvDetialsItemSongName.setText(dataEntity.getVideoName());
        holder.mvDetialsItemTime.setText(toTime(dataEntity.getMvList().get(0).getDuration()));

        Picasso.with(context).load(dataEntity.getMvList().get(0).getPicUrl()).placeholder(R.mipmap.placeholder_detail_radio_ban)
                .error(R.mipmap.placeholder_detail_radio_ban).into(holder.mvDetialsItemPicture);

        return convertView;
    }

    private class ViewHolder {
        private ImageView mvDetialsItemPicture;
        private TextView mvDetialsItemSongName, mvDetialsItemSingerName;
        private TextView mvDetialsItemPlayCount, mvDetialsItemCommentCount, mvDetialsItemTime;
    }
    public String toTime(int time) {

        time /= 1000;
        int minute = time / 60;
        int second = time % 60;
        minute %= 60;
        return String.format("%02d:%02d", minute, second);
    }
}
